package com.community.app.service.impl;

import com.community.app.entity.WxOwner;
import com.community.app.mapper.WxOwnerMapper;
import com.community.app.service.WxOwnerService;
import com.community.common.utils.SecurityUtils;
import com.community.utils.LongUUIDGenerator;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 业主 (ZyOwner)表服务实现类
 *
 * @author makejava
 * @since 2024-04-12 16:20:22
 */
@Service("zyOwnerService")
public class WxOwnerServiceImpl implements WxOwnerService {
    @Resource
    private WxOwnerMapper wxOwnerMapper;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param ownerId 主键
     * @return 实例对象
     */
    @Override
    public WxOwner queryById(Long ownerId) {
        return this.wxOwnerMapper.queryById(ownerId);
    }

    /**
     * 分页查询
     *
     * @param wxOwner     筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<WxOwner> queryByPage(WxOwner wxOwner, PageRequest pageRequest) {
        long total = this.wxOwnerMapper.count(wxOwner);
        return new PageImpl<>(this.wxOwnerMapper.queryAllByLimit(wxOwner, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param wxOwner 实例对象
     * @return 实例对象
     */
    @Override
    public WxOwner insert(WxOwner wxOwner) {
        this.wxOwnerMapper.insert(wxOwner);
        return wxOwner;
    }

    /**
     * 通过主键删除数据
     *
     * @param ownerId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long ownerId) {
        return this.wxOwnerMapper.deleteById(ownerId) > 0;
    }

    @Override
    public int login(WxOwner wxOwner) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String pwd = wxOwnerMapper.getPwd(wxOwner.getOwnerPhoneNumber());
        if (SecurityUtils.matchesPassword(wxOwner.getOwnerPassword(),pwd)) {
            ValueOperations<String, String> ops = redisTemplate.opsForValue();
            ops.set(wxOwner.getOwnerPhoneNumber(), uuid,60 * 30, TimeUnit.SECONDS);
            return 1;
        }
        return 0;
    }

    @Override
    public int register(WxOwner wxOwner) {
        // 使用加密后的密码替换原始密码
        wxOwner.setOwnerPassword(SecurityUtils.encryptPassword(wxOwner.getOwnerPassword()));
        // 设置创建时间为当前时间
        wxOwner.setCreateTime(new Date());
        // 设置性别为未知
        wxOwner.setOwnerGender("male");
        // 设置用户类型为"zu"
        wxOwner.setOwnerType("zu");
        // 生成一个短UUID作为用户昵称的一部分
        String uuid = Arrays.toString(LongUUIDGenerator.generateLongUUID()).substring(1, 6);
        wxOwner.setOwnerNickname("用户" + uuid);
        wxOwner.setOwnerLogonMode("weChat");
        wxOwner.setOwnerStatus("Enable");
        // 将用户信息插入到数据库并返回影响的行数
        return wxOwnerMapper.insert(wxOwner);
    }

    @Override
    public int selectByPhone(String ownerPhoneNumber) {
        return wxOwnerMapper.selectByPhone(ownerPhoneNumber);
    }

    @Override
    public WxOwner getOwnerByPhoneNumber(String ownerPhoneNumber) {
        return wxOwnerMapper.getOwnerByPhoneNumber(ownerPhoneNumber);
    }

    /**
     *  更新用户信息
     * @param wxOwner
     * @return
     */
    @Override
    public int updateOwner(WxOwner wxOwner) {
        wxOwner.setUpdateTime(new Date());
        return wxOwnerMapper.update(wxOwner);
    }

    /**
     *  实名认证
     */
    @Override
    public int realAuthentication(WxOwner wxOwner) {
        return wxOwnerMapper.realAuthentication(wxOwner);
    }

    /**
     * 修改用户密码
     */
    @Override
    public void updatePwd(WxOwner wxOwner) {
        wxOwner.setOwnerPassword(SecurityUtils.encryptPassword(wxOwner.getOwnerPassword()));
        wxOwnerMapper.updatePwd(wxOwner);
    }

    /**
     *  更新用户头像
     */
    @Override
    public boolean updateUserAvatar(String phoneNumber, String url) {
        return  wxOwnerMapper.updateUserAvatar(phoneNumber, url) > 0;
    }
}
